<template>
  <div class="p-4">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-form inline>
          <el-form-item>
            <el-button
              type="success"
              icon="el-icon-plus"
              @click="deBounceWrap(handleAdd)"
              >新增</el-button
            >
          </el-form-item>
        </el-form>
      </div>
      <el-table :data="data" border>
        <el-table-column prop="id" label="序号" width="150px"></el-table-column>
        <el-table-column prop="name" label="名称"></el-table-column>
        <el-table-column label="操作" width="250px">
          <template slot-scope="scope">
            <el-button
              type="primary"
              plain
              size="mini"
              icon="el-icon-edit"
              @click="deBounceWrap(handleEdit, scope.row)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              plain
              size="mini"
              icon="el-icon-delete"
              @click="deBounceWrap(handleDel, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- <cus-pagination
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.page_size"
        @pagination="getList"
      /> -->
    </el-card>
    <el-dialog
      :title="titleOptions[title]"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      width="40%"
      @close="handleClear"
      :before-close="handleClose"
    >
      <el-form :model="form" ref="form" :rules="rules" label-width="120px">
        <el-form-item
          prop="prize_pool_name"
          label="奖池名称"
          label-width="120px"
        >
          <el-input
            v-model.trim="form.prize_pool_name"
            placeholder="请输入奖池名称"
            clearable
          ></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button type="primary" plain @click="handleClose">返回</el-button>
        <el-button type="primary" @click="deBounceWrap(handleSumbit, 'form')"
          >提交</el-button
        >
      </div>
    </el-dialog>

    <!-- 礼物类型 -->
    <el-dialog
      title="确定删除吗?"
      :visible.sync="deleteDialogVisible"
      width="435px"
    >
      <span style=""
        ><em style="font-style: normal; color: red">删除后不可恢复</em
        >，你还要继续吗？</span
      >
      <div slot="footer" class="dialog-footer">
        <el-button @click="deleteDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmDelete">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  giveRecord,
  addPrizePool,
  editPrizePool,
  delPrizePool
} from "@/api/intimacyGift/propType";
export default {
  data() {
    return {
      data: [],
      listQuery: {
        page: 1,
        page_size: 10
      },
      total: 0,
      titleOptions: {
        add: "新增",
        edit: "编辑"
      },
      title: "add",
      rowData: {},
      form: {
        prize_pool_name: undefined
      },
      rules: {
        prize_pool_name: [
          { required: true, message: "请输入奖池名称", trigger: "blur" },
          { max: 20, message: "奖池名称不能超过20字符" }
        ]
      },
      //删除
      deleteDialogVisible: false,
      dialogVisible: false
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //列表
    getList() {
      giveRecord(this.listQuery).then(res => {
        this.data = res.data;
        //  this.total = res.data.total;
      });
    },

    //新增
    handleAdd() {
      this.title = "add";
      this.dialogVisible = true;
    },

    //编辑
    handleEdit(row) {
      this.title = "edit";
      this.rowData = row;
      this.form.prize_pool_name = JSON.parse(JSON.stringify(this.rowData.name));

      this.dialogVisible = true;
    },
    handleSumbit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.title === "add") {
            let data = {
              prize_pool_name: this.form.prize_pool_name
            };
            addPrizePool(data).then(res => {
              if (res.code === 200) {
                this.$message.success("提交成功");
                this.getList();
                this.dialogVisible = false;
              } else {
              }
            });
          }
          if (this.title === "edit") {
            let data = {
              id: this.rowData.id,
              prize_pool_name: this.form.prize_pool_name
            };
            editPrizePool(data).then(res => {
              if (res.code === 200) {
                this.$message.success("编辑成功");
                this.getList();
                this.dialogVisible = false;
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    //清空表单
    handleClose() {
      this.dialogVisible = false;
    },
    handleClear() {
      this.$refs["form"].resetFields();
      this.form = {
        prize_pool_name: undefined
      };
    },
    //删除
    handleDel(row) {
      this.rowData.id = row.id;
      this.deleteDialogVisible = true;
    },
    //确定删除
    confirmDelete() {
      delPrizePool({ id: this.rowData.id }).then(response => {
        if (response.code === 200) {
          this.getList();
          this.deleteDialogVisible = false;
          this.$notify({
            type: "success",
            message: "操作成功"
          });
        }
      });
    }
  }
};
</script>
<style lang="scss" scoped></style>
